Refactor PackageId.namespace -> source_id
authorYehuda Katz + Carl Lerche <engineering@tilde.io>
Tue, 8 Jul 2014 21:58:56 +0000 (14:58 -0700)
committerTim Carey-Smith <tim@spork.in>
Tue, 8 Jul 2014 21:58:56 +0000 (14:58 -0700)
src/bin/cargo-clean.rs
src/cargo/core/package_id.rs
src/cargo/core/resolver.rs
src/cargo/core/source.rs
src/cargo/util/toml.rs
tests/test_cargo_compile_git_deps.rs

index 7d9189c3cacc08a7ec89d1cacb6d269a73af2f85..4bd1e7484aaf390e66099b0373777cd821e83f0d 100644 (file)
@@ -1,4 +1,4 @@
-#![crate_id="cargo-clean"]
+#![crate_name="cargo-clean"]
 #![feature(phase)]
 
 extern crate cargo;
index aa03dd18512d49fb845e5d57e3601caf51ffe5f0..61dfec353b7bd9788117903133fbffd7cc7370ae 100644 (file)
@@ -10,7 +10,7 @@ use serialize::{
 };
 
 use util::{CargoResult, CargoError};
-use core::source::Location;
+use core::source::SourceId;
 
 trait ToVersion {
     fn to_version(self) -> Result<semver::Version, String>;
@@ -57,7 +57,7 @@ impl<'a> ToUrl for &'a Url {
 pub struct PackageId {
     name: String,
     version: semver::Version,
-    namespace: Location,
+    source_id: SourceId,
 }
 
 #[deriving(Clone, Show, PartialEq)]
@@ -78,12 +78,12 @@ impl CargoError for PackageIdError {
 
 impl PackageId {
     pub fn new<T: ToVersion>(name: &str, version: T,
-                             ns: &Location) -> CargoResult<PackageId> {
+                             sid: &SourceId) -> CargoResult<PackageId> {
         let v = try!(version.to_version().map_err(InvalidVersion));
         Ok(PackageId {
             name: name.to_str(),
             version: v,
-            namespace: ns.clone()
+            source_id: sid.clone()
         })
     }
 
@@ -95,8 +95,8 @@ impl PackageId {
         &self.version
     }
 
-    pub fn get_namespace<'a>(&'a self) -> &'a Location {
-        &self.namespace
+    pub fn get_source_id<'a>(&'a self) -> &'a SourceId {
+        &self.source_id
     }
 }
 
@@ -106,8 +106,8 @@ impl Show for PackageId {
     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
         try!(write!(f, "{} v{}", self.name, self.version));
 
-        if self.namespace.to_str().as_slice() != central_repo {
-            try!(write!(f, " ({})", self.namespace));
+        if self.source_id.to_str().as_slice() != central_repo {
+            try!(write!(f, " ({})", self.source_id));
         }
 
         Ok(())
@@ -118,31 +118,29 @@ impl<D: Decoder<Box<CargoError + Send>>>
     Decodable<D,Box<CargoError + Send>>
     for PackageId
 {
-    fn decode(d: &mut D) -> Result<PackageId, Box<CargoError + Send>> {
-        let vector: Vec<String> = try!(Decodable::decode(d));
+    fn decode(d: &mut D) -> CargoResult<PackageId> {
+        let (name, version, source_id): (String, String, SourceId) = try!(Decodable::decode(d));
 
-        PackageId::new(
-            vector.get(0).as_slice(),
-            vector.get(1).as_slice(),
-            &try!(Location::parse(vector.get(2).as_slice())))
+        PackageId::new(name.as_slice(), version.as_slice(), &source_id)
     }
 }
 
 impl<E, S: Encoder<E>> Encodable<S,E> for PackageId {
     fn encode(&self, e: &mut S) -> Result<(), E> {
-        (vec!(self.name.clone(), self.version.to_str()),
-              self.namespace.to_str()).encode(e)
+        (self.name.clone(), self.version.to_str(), self.source_id.clone()).encode(e)
     }
 }
 
 #[cfg(test)]
 mod tests {
     use super::{PackageId, central_repo};
-    use core::source::Location;
+    use core::source::{Location, RegistryKind, SourceId};
 
     #[test]
     fn invalid_version_handled_nicely() {
-        let repo = Location::parse(central_repo).unwrap();
+        let loc = Location::parse(central_repo).unwrap();
+        let repo = SourceId::new(RegistryKind, loc);
+
         assert!(PackageId::new("foo", "1.0", &repo).is_err());
         assert!(PackageId::new("foo", "1", &repo).is_err());
         assert!(PackageId::new("foo", "bar", &repo).is_err());
index 3ca67c7091f316733781aca77100eecc73d47864..968fec3dc6fc1ce1127a95106f69cd15ee45c7c4 100644 (file)
@@ -4,7 +4,7 @@ use core::{
     Dependency,
     PackageId,
     Summary,
-    Registry
+    Registry,
 };
 
 use util::{CargoResult, human, internal};
@@ -96,8 +96,9 @@ mod test {
         )
     )
 
-    fn registry_loc() -> Location {
-        Location::parse("http://www.example.com/").unwrap()
+    fn registry_loc() -> SourceId {
+        let remote = Location::parse("http://www.example.com/").unwrap();
+        SourceId::new(RegistryKind, remote)
     }
 
     fn pkg(name: &str) -> Summary {
index cbad3cd06f4039afb5930e04eae1165205ab4838..1046537af073abdd7b4a0ecc23919862877e9f2e 100644 (file)
@@ -1,12 +1,13 @@
 use std::fmt;
 use std::fmt::{Show, Formatter};
+use serialize::{Decodable, Decoder, Encodable, Encoder};
 
 use url::Url;
 
 use core::{Summary, Package, PackageId};
 use sources::{PathSource, GitSource};
 use sources::git;
-use util::{Config, CargoResult};
+use util::{Config, CargoResult, CargoError};
 use util::errors::human;
 
 /// A Source finds and downloads remote packages based on names and
@@ -45,7 +46,7 @@ pub trait Source {
     fn fingerprint(&self, pkg: &Package) -> CargoResult<String>;
 }
 
-#[deriving(Show, Clone, PartialEq, Eq, PartialOrd, Ord)]
+#[deriving(Encodable, Decodable, Show, Clone, PartialEq, Eq, PartialOrd, Ord)]
 pub enum SourceKind {
     /// GitKind(<git reference>) represents a git repository
     GitKind(String),
@@ -61,7 +62,22 @@ pub enum Location {
     Remote(Url),
 }
 
-#[deriving(Clone, Eq)]
+type Error = Box<CargoError + Send>;
+
+impl<E, D: Decoder<E>> Decodable<D, E> for Location {
+    fn decode(d: &mut D) -> Result<Location, E> {
+        let url: String  = raw_try!(Decodable::decode(d));
+        Ok(Location::parse(url.as_slice()).unwrap())
+    }
+}
+
+impl<E, S: Encoder<E>> Encodable<S, E> for Location {
+    fn encode(&self, e: &mut S) -> Result<(), E> {
+        self.to_str().encode(e)
+    }
+}
+
+#[deriving(Encodable, Decodable, Clone, Eq)]
 pub struct SourceId {
     pub kind: SourceKind,
     pub location: Location,
@@ -97,7 +113,7 @@ impl Show for SourceId {
             SourceId { kind: GitKind(ref reference), ref location } => {
                 try!(write!(f, "{}", location));
                 if reference.as_slice() != "master" {
-                    try!(write!(f, " (ref={})", reference));
+                    try!(write!(f, "#ref={}", reference));
                 }
             },
             SourceId { kind: RegistryKind, .. } => {
index 701a5274673ab889b9454b6b528eb02ecf4399a5..c42fc5f1c4a50c509baa6523d8ee474581f253a3 100644 (file)
@@ -39,7 +39,7 @@ pub fn project_layout(root: &Path) -> Layout {
         bins.push(root.join("src/main.rs"));
     }
 
-    fs::readdir(&root.join("src/bin"))
+    let _ = fs::readdir(&root.join("src/bin"))
         .map(|v| v.move_iter())
         .map(|i| i.filter(|f| f.extension_str() == Some("rs")))
         .map(|mut i| i.collect())
@@ -176,8 +176,8 @@ pub enum TomlBuildCommandsList {
 }
 
 impl TomlProject {
-    pub fn to_package_id(&self, namespace: &Location) -> CargoResult<PackageId> {
-        PackageId::new(self.name.as_slice(), self.version.as_slice(), namespace)
+    pub fn to_package_id(&self, source_id: &SourceId) -> CargoResult<PackageId> {
+        PackageId::new(self.name.as_slice(), self.version.as_slice(), source_id)
     }
 }
 
@@ -301,7 +301,7 @@ impl TomlManifest {
             try!(process_dependencies(&mut cx, true, self.dev_dependencies.as_ref()));
         }
 
-        let pkgid = try!(project.to_package_id(source_id.get_location()));
+        let pkgid = try!(project.to_package_id(source_id));
         let summary = Summary::new(&pkgid, deps.as_slice());
         Ok((Manifest::new(
                 &summary,
index c44f1c860a4eadf9de8e0f47abfe18c75be18a31..58e57dcf541b7f63dc70a816066e926c72949c3a 100644 (file)
@@ -149,7 +149,7 @@ test!(cargo_compile_git_dep_branch {
     assert_that(project.cargo_process("cargo-build"),
         execs()
         .with_stdout(format!("{} git repository `file:{}`\n\
-                              {} dep1 v0.5.0 (file:{})\n\
+                              {} dep1 v0.5.0 (file:{}#ref=branchy)\n\
                               {} foo v0.5.0 (file:{})\n",
                              UPDATING, git_root.display(),
                              COMPILING, git_root.display(),
@@ -214,7 +214,7 @@ test!(cargo_compile_git_dep_tag {
     assert_that(project.cargo_process("cargo-build"),
         execs()
         .with_stdout(format!("{} git repository `file:{}`\n\
-                              {} dep1 v0.5.0 (file:{})\n\
+                              {} dep1 v0.5.0 (file:{}#ref=v0.1.0)\n\
                               {} foo v0.5.0 (file:{})\n",
                              UPDATING, git_root.display(),
                              COMPILING, git_root.display(),